//
//  CHNoDockingDetailController.m
//  CarFile
//
//  Created by 陈浩 on 2018/4/27.
//  Copyright © 2018年 陈浩. All rights reserved.
//

#import "CHNoDockingDetailController.h"
#import "CHDockCell.h"
#import "DJ_Model.h"
#import "QY_Model.h"
@interface CHNoDockingDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) int page;    // 页码
@property (nonatomic ,strong) DJ_Model *model;
@property (nonatomic ,copy) NSString *levels;
@end

@implementation CHNoDockingDetailController

-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"联网对接详情";
    self.view.backgroundColor = KRGB(239, 239, 249);
    
    
    
    
    [self setupTableView];
    
    
}

-(void)setupTableView{
    

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenW - 30, 44)];
    titleLabel.text = [NSString stringWithFormat:@"%@有企业%@家，截止到目前，有%@家企业未完成系统对接，未对接率%@。",self.model.areacn,self.model.comnum,self.model.nodockcomnum,self.model.nodockcomrate];
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont systemFontOfSize:13];
    titleLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    [self.view addSubview:titleLabel];
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, UIScreenW, UIScreenH-64-44) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = KRGB(239, 239, 249);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        
        [self getData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    self.tableView.mj_footer.automaticallyHidden = self.datas.count > 0 ? NO : YES;
    [self.tableView.mj_header beginRefreshing];
    [self.view addSubview:self.tableView];
    
}

-(void)loadMoreData{
    _page++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"levels"] = self.levels;
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovnodocklistbycompany" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.tableView.mj_footer endRefreshing];
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                QY_Model *model = [QY_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            [self.tableView reloadData];
        }
    }];
    
    
}



-(void)getData{
    NSLog(@"业务类别%@",self.levels);
    _page = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"levels"] = self.levels;
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    NSLog(@"%@",params);
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovnodocklistbycompany" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",result);
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                QY_Model *model = [QY_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            self.tableView.placeholderImage = @"data_null";
            self.tableView.placeholder = @"暂时没有数据哦~";
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datas.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHDockCell *cell = [CHDockCell cellForTableView:tableView];
    cell.model = self.datas[indexPath.row];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = [LongriseRouter openURL:@"CHRepairDetailController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            QY_Model *model = self.datas[indexPath.row];
            params[@"companyid"] = model.companyid;
            params[@"areaid"] = model.areaid;
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:params block:^(id obj) {
                
                
            }];
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        NSMutableDictionary *dict = obj;
        self.model = dict[@"model"];
        self.levels = dict[@"levels"];
    }
}


@end
